home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Applications / Portable Patmos / var / tmp / cc000047.i < prev    next >
Encoding:
Text File  |  1995-10-25  |  15.8 KB  |  1,493 lines

  1. # 1 "unlzh.c"
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11. # 1 "/usr/include/stdio.h" 1 3
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54. # 1 "/usr/include/sys/cdefs.h" 1 3
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119. # 82 "/usr/include/sys/cdefs.h" 3
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149. # 43 "/usr/include/stdio.h" 2 3
  150.  
  151.  
  152. # 1 "/usr/include/machine/ansi.h" 1 3
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209.  
  210.  
  211. # 45 "/usr/include/stdio.h" 2 3
  212.  
  213.  
  214. typedef    unsigned int            size_t;
  215.  
  216.  
  217.  
  218.  
  219.  
  220.  
  221.  
  222. typedef long fpos_t;         
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
  233. struct __sbuf {
  234.     unsigned char *_base;
  235.     int    _size;
  236. };
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262. typedef    struct __sFILE {
  263.     unsigned char *_p;     
  264.     int    _r;         
  265.     int    _w;         
  266.     short    _flags;         
  267.     short    _file;         
  268.     struct    __sbuf _bf;     
  269.     int    _lbfsize;     
  270.  
  271.      
  272.     void    *_cookie;     
  273.     int    (*_close)  (void *)         ;
  274.     int    (*_read)   (void *, char *, int)         ;
  275.     fpos_t    (*_seek)   (void *, fpos_t, int)         ;
  276.     int    (*_write)  (void *, const char *, int)         ;
  277.  
  278.      
  279.     struct    __sbuf _ub;     
  280.     unsigned char *_up;     
  281.     int    _ur;         
  282.  
  283.      
  284.     unsigned char _ubuf[3];     
  285.     unsigned char _nbuf[1];     
  286.  
  287.      
  288.     struct    __sbuf _lb;     
  289.  
  290.      
  291.     int    _blksize;     
  292.     int    _offset;     
  293. } FILE;
  294.  
  295.  
  296. extern FILE __sF[];
  297.  
  298.  
  299.  
  300.  
  301.  
  302.  
  303.      
  304.  
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.  
  335.  
  336.  
  337.  
  338.  
  339.  
  340.  
  341.  
  342.  
  343.  
  344.  
  345.  
  346.  
  347.  
  348.  
  349.  
  350.  
  351.  
  352.  
  353.  
  354.  
  355.  
  356.  
  357.  
  358.  
  359.  
  360.  
  361.  
  362.  
  363.  
  364. void     clearerr  (FILE *)         ;
  365. int     fclose  (FILE *)         ;
  366. int     feof  (FILE *)         ;
  367. int     ferror  (FILE *)         ;
  368. int     fflush  (FILE *)         ;
  369. int     fgetc  (FILE *)         ;
  370. int     fgetpos  (FILE *, fpos_t *)         ;
  371. char    *fgets  (char *, size_t, FILE *)         ;
  372. FILE    *fopen  (const char *, const char *)         ;
  373. int     fprintf  (FILE *, const char *, ...)         ;
  374. int     fputc  (int, FILE *)         ;
  375. int     fputs  (const char *, FILE *)         ;
  376. size_t     fread  (void *, size_t, size_t, FILE *)         ;
  377. FILE    *freopen  (const char *, const char *, FILE *)         ;
  378. int     fscanf  (FILE *, const char *, ...)         ;
  379. int     fseek  (FILE *, long, int)         ;
  380. int     fsetpos  (FILE *, const fpos_t *)         ;
  381. long     ftell  (const FILE *)         ;
  382. size_t     fwrite  (const void *, size_t, size_t, FILE *)         ;
  383. int     getc  (FILE *)         ;
  384. int     getchar  (void)         ;
  385. char    *gets  (char *)         ;
  386.  
  387. extern int sys_nerr;             
  388. extern const char *const sys_errlist[];
  389.  
  390. void     perror  (const char *)         ;
  391. int     printf  (const char *, ...)         ;
  392. int     putc  (int, FILE *)         ;
  393. int     putchar  (int)         ;
  394. int     puts  (const char *)         ;
  395. int     remove  (const char *)         ;
  396. int     rename   (const char *, const char *)         ;
  397. void     rewind  (FILE *)         ;
  398. int     scanf  (const char *, ...)         ;
  399. void     setbuf  (FILE *, char *)         ;
  400. int     setvbuf  (FILE *, char *, int, size_t)         ;
  401. int     sprintf  (char *, const char *, ...)         ;
  402. int     sscanf  (const char *, const char *, ...)         ;
  403. FILE    *tmpfile  (void)         ;
  404. char    *tmpnam  (char *)         ;
  405. int     ungetc  (int, FILE *)         ;
  406. int     vfprintf  (FILE *, const char *, char *            )         ;
  407. int     vprintf  (const char *, char *            )         ;
  408. int     vsprintf  (char *, const char *, char *            )         ;
  409.  
  410.  
  411.  
  412.  
  413.  
  414.  
  415.  
  416.  
  417.  
  418.  
  419. char    *ctermid  (char *)         ;
  420. char    *cuserid  (char *)         ;
  421. FILE    *fdopen  (int, const char *)         ;
  422. int     fileno  (FILE *)         ;
  423.  
  424.  
  425.  
  426.  
  427.  
  428.  
  429.  
  430.  
  431. char    *fgetline  (FILE *, size_t *)         ;
  432. int     fpurge  (FILE *)         ;
  433. int     getw  (FILE *)         ;
  434. int     pclose  (FILE *)         ;
  435. FILE    *popen  (const char *, const char *)         ;
  436. int     putw  (int, FILE *)         ;
  437. void     setbuffer  (FILE *, char *, int)         ;
  438. int     setlinebuf  (FILE *)         ;
  439. char    *tempnam  (const char *, const char *)         ;
  440. int     snprintf  (char *, size_t, const char *, ...)         
  441.         __attribute__((format (printf, 3, 4)));
  442. int     vsnprintf  (char *, size_t, const char *, char *            )         
  443.         __attribute__((format (printf, 3, 0)));
  444. int     vscanf  (const char *, char *            )         
  445.         __attribute__((format (scanf, 1, 0)));
  446. int     vsscanf  (const char *, const char *, char *            )         
  447.         __attribute__((format (scanf, 2, 0)));
  448.  
  449.  
  450.  
  451.  
  452.  
  453.  
  454.  
  455.  
  456.  
  457.  
  458.  
  459.  
  460.  
  461. FILE    *funopen  (const void *,
  462.         int (*)(void *, char *, int),
  463.         int (*)(void *, const char *, int),
  464.         fpos_t (*)(void *, fpos_t, int),
  465.         int (*)(void *))         ;
  466.  
  467.  
  468.  
  469.  
  470.  
  471.  
  472.  
  473.  
  474.  
  475. int    __srget  (FILE *)         ;
  476. int    __svfscanf  (FILE *, const char *, char *            )         ;
  477. int    __swbuf  (int, FILE *)         ;
  478.  
  479.  
  480.  
  481.  
  482.  
  483.  
  484.  
  485.  
  486. static __inline int __sputc(int _c, FILE *_p) {
  487.     if (--_p->_w >= 0 || (_p->_w >= _p->_lbfsize && (char)_c != '\n'))
  488.         return (*_p->_p++ = _c);
  489.     else
  490.         return (__swbuf(_c, _p));
  491. }
  492. # 337 "/usr/include/stdio.h" 3
  493.  
  494.  
  495.  
  496.  
  497.  
  498.  
  499.  
  500.  
  501.  
  502.  
  503.  
  504.  
  505.  
  506.  
  507.  
  508.  
  509.  
  510.  
  511.  
  512.  
  513.  
  514.  
  515.  
  516. # 10 "unlzh.c" 2
  517.  
  518.  
  519. # 1 "tailor.h" 1
  520.  
  521.  
  522.  
  523.  
  524.  
  525.  
  526.  
  527.  
  528.  
  529.  
  530.  
  531.  
  532.  
  533.  
  534.  
  535.  
  536.  
  537.  
  538.  
  539.  
  540.  
  541.  
  542.  
  543.  
  544. # 65 "tailor.h"
  545.  
  546.  
  547.  
  548.  
  549. # 109 "tailor.h"
  550.  
  551.  
  552. # 130 "tailor.h"
  553.  
  554.  
  555. # 144 "tailor.h"
  556.  
  557.  
  558.  
  559.  
  560.  
  561.  
  562.  
  563.  
  564.  
  565. # 172 "tailor.h"
  566.  
  567.  
  568. # 194 "tailor.h"
  569.  
  570.  
  571. # 215 "tailor.h"
  572.  
  573.  
  574. # 230 "tailor.h"
  575.  
  576.  
  577. # 250 "tailor.h"
  578.  
  579.  
  580.  
  581.  
  582.  
  583.  
  584.  
  585.  
  586.  
  587.  
  588.  
  589.  
  590.  
  591.  
  592.  
  593.      
  594.  
  595.  
  596.  
  597.  
  598.  
  599.  
  600.  
  601.  
  602.  
  603.  
  604.  
  605.  
  606.  
  607.  
  608.  
  609.  
  610.  
  611.  
  612.  
  613.  
  614.  
  615.  
  616.  
  617.  
  618.  
  619.  
  620.  
  621.  
  622.  
  623.  
  624.  
  625.  
  626.  
  627.  
  628.  
  629.  
  630.  
  631.     
  632.  
  633.  
  634.  
  635.  
  636.  
  637.  
  638.  
  639.  
  640.  
  641.  
  642.  
  643.  
  644.  
  645.  
  646.  
  647.  
  648.  
  649.  
  650.  
  651.  
  652.  
  653.  
  654.  
  655.  
  656.  
  657. # 12 "unlzh.c" 2
  658.  
  659. # 1 "gzip.h" 1
  660.  
  661.  
  662.  
  663.  
  664.  
  665.  
  666.  
  667.  
  668.  
  669.  
  670.  
  671.  
  672.  
  673.    typedef void *voidp;
  674.  
  675.  
  676.  
  677.  
  678.  
  679.  
  680.  
  681.  
  682.  
  683. # 1 "/usr/include/string.h" 1 3
  684.  
  685.  
  686.  
  687.  
  688.  
  689.  
  690.  
  691.  
  692.  
  693.  
  694.  
  695.  
  696.  
  697.  
  698.  
  699.  
  700.  
  701.  
  702.  
  703.  
  704.  
  705.  
  706.  
  707.  
  708.  
  709.  
  710.  
  711.  
  712.  
  713.  
  714.  
  715.  
  716.  
  717.  
  718.  
  719.  
  720.  
  721.  
  722.  
  723.  
  724.  
  725.  
  726.  
  727.  
  728.  
  729.  
  730.  
  731.  
  732.  
  733.  
  734.  
  735.  
  736. void    *memchr  (const void *, int, size_t)         ;
  737.  
  738.  
  739.  
  740.  
  741. void    *memmove  (void *, const void *, size_t)         ;
  742. void    *memset  (void *, int, size_t)         ;
  743. char    *strcat  (char *, const char *)         ;
  744. char    *strchr  (const char *, int)         ;
  745. int     strcmp  (const char *, const char *)         ;
  746. int     strcoll  (const char *, const char *)         ;
  747. char    *strcpy  (char *, const char *)         ;
  748. size_t     strcspn  (const char *, const char *)         ;
  749. char    *strerror  (int)         ;
  750. size_t     strlen  (const char *)         ;
  751. char    *strncat  (char *, const char *, size_t)         ;
  752. int     strncmp  (const char *, const char *, size_t)         ;
  753. char    *strncpy  (char *, const char *, size_t)         ;
  754. char    *strpbrk  (const char *, const char *)         ;
  755. char    *strrchr  (const char *, int)         ;
  756. size_t     strspn  (const char *, const char *)         ;
  757. char    *strstr  (const char *, const char *)         ;
  758. char    *strtok  (char *, const char *)         ;
  759. size_t     strxfrm  (char *, const char *, size_t)         ;
  760.  
  761.  
  762.  
  763. int     bcmp  (const void *, const void *, size_t)         ;
  764. void     bcopy  (const void *, void *, size_t)         ;
  765. void     bzero  (void *, size_t)         ;
  766. int     ffs  (int)         ;
  767. char    *index  (const char *, int)         ;
  768. void    *memccpy  (void *, const void *, int, size_t)         ;
  769. char    *rindex  (const char *, int)         ;
  770. int     strcasecmp  (const char *, const char *)         ;
  771. char    *strdup  (const char *)         ;
  772. void     strmode  (int, char *)         ;
  773. int     strncasecmp  (const char *, const char *, size_t)         ;
  774. char    *strsep  (char **, const char *)         ;
  775. void     swab  (const void *, void *, size_t)         ;
  776.  
  777.  
  778.  
  779.  
  780. # 24 "gzip.h" 2
  781.  
  782.  
  783.  
  784.  
  785.  
  786.  
  787.  
  788.  
  789.  
  790.  
  791.  
  792.  
  793.  
  794.  
  795.  
  796.  
  797.  
  798.  
  799.  
  800.  
  801. typedef unsigned char  uch;
  802. typedef unsigned short ush;
  803. typedef unsigned long  ulg;
  804.  
  805.  
  806.  
  807.  
  808.  
  809.  
  810.  
  811.  
  812.  
  813.  
  814.  
  815.  
  816.  
  817.  
  818. extern int method;          
  819.  
  820.  
  821.  
  822.  
  823.  
  824.  
  825.  
  826.  
  827.  
  828.  
  829.  
  830.  
  831.  
  832.  
  833.  
  834.  
  835.  
  836.  
  837.  
  838.  
  839.  
  840.  
  841.  
  842.  
  843.  
  844.  
  845.  
  846.  
  847.  
  848.  
  849.  
  850.  
  851.  
  852.  
  853.  
  854.  
  855.  
  856.  
  857. # 108 "gzip.h"
  858.  
  859.  
  860.  
  861.  
  862.  
  863.  
  864.  
  865.  extern  uch    inbuf [] ;           
  866.  extern  uch    outbuf [] ;          
  867.  extern  ush    d_buf [] ;           
  868.  extern  uch    window [] ;          
  869.  
  870.  
  871.  
  872.  
  873.     extern  ush    prev     [] ;   
  874.  
  875.  
  876.  
  877.  
  878.  
  879.  
  880.  
  881. extern unsigned insize;  
  882. extern unsigned inptr;   
  883. extern unsigned outcnt;  
  884.  
  885. extern long bytes_in;    
  886. extern long bytes_out;   
  887. extern long header_bytes; 
  888.  
  889.  
  890.  
  891.  
  892. extern int  ifd;         
  893. extern int  ofd;         
  894. extern char ifname[];    
  895. extern char ofname[];    
  896. extern char *progname;   
  897.  
  898. extern long time_stamp;  
  899. extern long ifile_size;  
  900.  
  901. typedef int file_t;      
  902.  
  903.  
  904.  
  905.  
  906.  
  907.  
  908.  
  909.  
  910.  
  911.  
  912.  
  913.  
  914.  
  915.  
  916.  
  917.  
  918.  
  919.  
  920.  
  921.  
  922.  
  923.  
  924.  
  925.  
  926.  
  927.  
  928.  
  929.  
  930.  
  931.  
  932.  
  933.  
  934.  
  935.  
  936.  
  937.  
  938.  
  939.  
  940.  
  941.  
  942.  
  943. extern int decrypt;         
  944. extern int exit_code;       
  945. extern int verbose;         
  946. extern int quiet;           
  947. extern int level;           
  948. extern int test;            
  949. extern int to_stdout;       
  950. extern int save_orig_name;  
  951.  
  952.  
  953.  
  954.  
  955.  
  956.  
  957.  
  958.  
  959.  
  960.  
  961.  
  962.  
  963.  
  964.  
  965.  
  966.  
  967. # 225 "gzip.h"
  968.  
  969.  
  970.  
  971.  
  972.  
  973.  
  974.  
  975.  
  976.  
  977.  
  978.  
  979.  
  980.  
  981.  
  982.  
  983.  
  984.  
  985.  
  986.  
  987.  
  988.  
  989.  
  990.  
  991.  
  992.  
  993.  
  994.  
  995.  
  996.  
  997.  
  998.  
  999.  
  1000.  
  1001.  
  1002.  
  1003.  
  1004.      
  1005. extern int zip          (int in, int out)  ;
  1006. extern int file_read    (char *buf,  unsigned size)  ;
  1007.  
  1008.      
  1009. extern int unzip        (int in, int out)  ;
  1010. extern int check_zipfile   (int in)  ;
  1011.  
  1012.      
  1013. extern int unpack       (int in, int out)  ;
  1014.  
  1015.      
  1016. extern int unlzh        (int in, int out)  ;
  1017.  
  1018.      
  1019. void  abort_gzip   (void)  ;
  1020.  
  1021.          
  1022. void lm_init   (int pack_level, ush *flags)  ;
  1023. ulg  deflate   (void)  ;
  1024.  
  1025.          
  1026. void ct_init       (ush *attr, int *method)  ;
  1027. int  ct_tally      (int dist, int lc)  ;
  1028. ulg  flush_block   (char *buf, ulg stored_len, int eof)  ;
  1029.  
  1030.          
  1031. void     bi_init      (file_t zipfile)  ;
  1032. void     send_bits    (int value, int length)  ;
  1033. unsigned bi_reverse   (unsigned value, int length)  ;
  1034. void     bi_windup    (void)  ;
  1035. void     copy_block   (char *buf, unsigned len, int header)  ;
  1036. extern   int (*read_buf)   (char *buf, unsigned size)  ;
  1037.  
  1038.      
  1039. extern int copy             (int in, int out)  ;
  1040. extern ulg  updcrc          (uch *s, unsigned n)  ;
  1041. extern void clear_bufs      (void)  ;
  1042. extern int  fill_inbuf      (int eof_ok)  ;
  1043. extern void flush_outbuf    (void)  ;
  1044. extern void flush_window    (void)  ;
  1045. extern void write_buf       (int fd, voidp buf, unsigned cnt)  ;
  1046. extern char *strlwr         (char *s)  ;
  1047. extern char *basename       (char *fname)  ;
  1048. extern void make_simple_name   (char *name)  ;
  1049. extern char *add_envopt     (int *argcp, char ***argvp, char *env)  ;
  1050. extern void error           (char *m)  ;
  1051. extern void warn            (char *a, char *b)  ;
  1052. extern void read_error      (void)  ;
  1053. extern void write_error     (void)  ;
  1054. extern void display_ratio   (long num, long den, FILE *file)  ;
  1055. extern voidp xmalloc        (unsigned int size)  ;
  1056.  
  1057.      
  1058. extern int inflate   (void)  ;
  1059. # 13 "unlzh.c" 2
  1060.  
  1061. # 1 "lzw.h" 1
  1062.  
  1063.  
  1064.  
  1065.  
  1066.  
  1067.  
  1068.  
  1069.  
  1070.  
  1071.  
  1072.  
  1073.  
  1074.  
  1075.  
  1076.  
  1077.  
  1078.  
  1079.  
  1080.  
  1081.  
  1082.  
  1083.  
  1084.  
  1085.  
  1086.  
  1087.  
  1088.  
  1089.  
  1090.  
  1091.  
  1092.  
  1093.  
  1094.  
  1095.  
  1096.  
  1097.  
  1098.  
  1099. extern int maxbits;       
  1100. extern int block_mode;    
  1101.  
  1102. extern int lzw      (int in, int out)  ;
  1103. extern int unlzw    (int in, int out)  ;
  1104. # 14 "unlzh.c" 2
  1105.  
  1106.  
  1107.  
  1108.  
  1109. static  unsigned  decode    (unsigned count, uch buffer[])  ;
  1110. static  void decode_start   (void)  ;
  1111.  
  1112.  
  1113. static  void huf_decode_start   (void)  ;
  1114. static  unsigned decode_c       (void)  ;
  1115. static  unsigned decode_p       (void)  ;
  1116. static  void read_pt_len        (int nn, int nbit, int i_special)  ;
  1117. static  void read_c_len         (void)  ;
  1118.  
  1119.  
  1120. static  void fillbuf        (int n)  ;
  1121. static  unsigned getbits    (int n)  ;
  1122. static  void init_getbits   (void)  ;
  1123.  
  1124.  
  1125.  
  1126. static  void make_table   (int nchar, uch bitlen[],
  1127.               int tablebits, ush table[])  ;
  1128.  
  1129.  
  1130.  
  1131.  
  1132.  
  1133.  
  1134.  
  1135.  
  1136.  
  1137.  
  1138.  
  1139.  
  1140.  
  1141.  
  1142.  
  1143.  
  1144.  
  1145.  
  1146.  
  1147.  
  1148.  
  1149.  
  1150.  
  1151.  
  1152.  
  1153.  
  1154.      
  1155.  
  1156.  
  1157.  
  1158.  
  1159.  
  1160.  
  1161.  
  1162.  
  1163.  
  1164.  
  1165.  
  1166.  
  1167.  
  1168.  
  1169.  
  1170.  
  1171.  
  1172.  
  1173.  
  1174.  
  1175.  
  1176.  
  1177.  
  1178.  
  1179.  
  1180.  
  1181.  
  1182. static  uch pt_len[( 16   + 3)  ];
  1183. static  unsigned blocksize;
  1184. static  ush pt_table[256];
  1185.  
  1186.  
  1187.  
  1188.  
  1189.  
  1190.  
  1191.  
  1192.  
  1193.  
  1194.  
  1195.  
  1196. static  ush       bitbuf;
  1197. static  unsigned  subbitbuf;
  1198. static  int       bitcount;
  1199.  
  1200. static  void fillbuf(n)   
  1201.     int n;
  1202. {
  1203.     bitbuf <<= n;
  1204.     while (n > bitcount) {
  1205.     bitbuf |= subbitbuf << (n -= bitcount);
  1206.     subbitbuf = (unsigned) (inptr < insize ? inbuf[inptr++] : fill_inbuf(1)) ;
  1207.     if ((int)subbitbuf == (-1) ) subbitbuf = 0;
  1208.     bitcount = 8 ;
  1209.     }
  1210.     bitbuf |= subbitbuf >> (bitcount -= n);
  1211. }
  1212.  
  1213. static  unsigned getbits(n)
  1214.     int n;
  1215. {
  1216.     unsigned x;
  1217.  
  1218.     x = bitbuf >> ((8  * 2 * sizeof(char))  - n);  fillbuf(n);
  1219.     return x;
  1220. }
  1221.  
  1222. static  void init_getbits()
  1223. {
  1224.     bitbuf = 0;  subbitbuf = 0;  bitcount = 0;
  1225.     fillbuf((8  * 2 * sizeof(char)) );
  1226. }
  1227.  
  1228.  
  1229.  
  1230.  
  1231.  
  1232. static  void make_table(nchar, bitlen, tablebits, table)
  1233.     int nchar;
  1234.     uch bitlen[];
  1235.     int tablebits;
  1236.     ush table[];
  1237. {
  1238.     ush count[17], weight[17], start[18], *p;
  1239.     unsigned i, k, len, ch, jutbits, avail, nextcode, mask;
  1240.  
  1241.     for (i = 1; i <= 16; i++) count[i] = 0;
  1242.     for (i = 0; i < (unsigned)nchar; i++) count[bitlen[i]]++;
  1243.  
  1244.     start[1] = 0;
  1245.     for (i = 1; i <= 16; i++)
  1246.     start[i + 1] = start[i] + (count[i] << (16 - i));
  1247.     if ((start[17] & 0xffff) != 0)
  1248.     error("Bad table\n");
  1249.  
  1250.     jutbits = 16 - tablebits;
  1251.     for (i = 1; i <= (unsigned)tablebits; i++) {
  1252.     start[i] >>= jutbits;
  1253.     weight[i] = (unsigned) 1 << (tablebits - i);
  1254.     }
  1255.     while (i <= 16) {
  1256.     weight[i] = (unsigned) 1 << (16 - i);
  1257.     i++;
  1258.     }
  1259.  
  1260.     i = start[tablebits + 1] >> jutbits;
  1261.     if (i != 0) {
  1262.     k = 1 << tablebits;
  1263.     while (i != k) table[i++] = 0;
  1264.     }
  1265.  
  1266.     avail = nchar;
  1267.     mask = (unsigned) 1 << (15 - tablebits);
  1268.     for (ch = 0; ch < (unsigned)nchar; ch++) {
  1269.     if ((len = bitlen[ch]) == 0) continue;
  1270.     nextcode = start[len] + weight[len];
  1271.     if (len <= (unsigned)tablebits) {
  1272.         for (i = start[len]; i < nextcode; i++) table[i] = ch;
  1273.     } else {
  1274.         k = start[len];
  1275.         p = &table[k >> jutbits];
  1276.         i = len - tablebits;
  1277.         while (i != 0) {
  1278.         if (*p == 0) {
  1279.             (prev+0x8000     )   [avail] =  prev [avail] = 0;
  1280.             *p = avail++;
  1281.         }
  1282.         if (k & mask) p = &(prev+0x8000     )   [*p];
  1283.         else          p = & prev [*p];
  1284.         k <<= 1;  i--;
  1285.         }
  1286.         *p = ch;
  1287.     }
  1288.     start[len] = nextcode;
  1289.     }
  1290. }
  1291.  
  1292.  
  1293.  
  1294.  
  1295.  
  1296. static  void read_pt_len(nn, nbit, i_special)
  1297.     int nn;
  1298.     int nbit;
  1299.     int i_special;
  1300. {
  1301.     int i, c, n;
  1302.     unsigned mask;
  1303.  
  1304.     n = getbits(nbit);
  1305.     if (n == 0) {
  1306.     c = getbits(nbit);
  1307.     for (i = 0; i < nn; i++) pt_len[i] = 0;
  1308.     for (i = 0; i < 256; i++) pt_table[i] = c;
  1309.     } else {
  1310.     i = 0;
  1311.     while (i < n) {
  1312.         c = bitbuf >> ((8  * 2 * sizeof(char))  - 3);
  1313.         if (c == 7) {
  1314.         mask = (unsigned) 1 << ((8  * 2 * sizeof(char))  - 1 - 3);
  1315.         while (mask & bitbuf) {  mask >>= 1;  c++;  }
  1316.         }
  1317.         fillbuf((c < 7) ? 3 : c - 3);
  1318.         pt_len[i++] = c;
  1319.         if (i == i_special) {
  1320.         c = getbits(2);
  1321.         while (--c >= 0) pt_len[i++] = 0;
  1322.         }
  1323.     }
  1324.     while (i < nn) pt_len[i++] = 0;
  1325.     make_table(nn, pt_len, 8, pt_table);
  1326.     }
  1327. }
  1328.  
  1329. static  void read_c_len()
  1330. {
  1331.     int i, c, n;
  1332.     unsigned mask;
  1333.  
  1334.     n = getbits(9  );
  1335.     if (n == 0) {
  1336.     c = getbits(9  );
  1337.     for (i = 0; i < (255  + 256     + 2 -  3    ) ; i++) outbuf [i] = 0;
  1338.     for (i = 0; i < 4096; i++) d_buf [i] = c;
  1339.     } else {
  1340.     i = 0;
  1341.     while (i < n) {
  1342.         c = pt_table[bitbuf >> ((8  * 2 * sizeof(char))  - 8)];
  1343.         if (c >= ( 16   + 3) ) {
  1344.         mask = (unsigned) 1 << ((8  * 2 * sizeof(char))  - 1 - 8);
  1345.         do {
  1346.             if (bitbuf & mask) c = (prev+0x8000     )   [c];
  1347.             else               c =  prev  [c];
  1348.             mask >>= 1;
  1349.         } while (c >= ( 16   + 3) );
  1350.         }
  1351.         fillbuf((int) pt_len[c]);
  1352.         if (c <= 2) {
  1353.         if      (c == 0) c = 1;
  1354.         else if (c == 1) c = getbits(4) + 3;
  1355.         else             c = getbits(9  ) + 20;
  1356.         while (--c >= 0) outbuf [i++] = 0;
  1357.         } else outbuf [i++] = c - 2;
  1358.     }
  1359.     while (i < (255  + 256     + 2 -  3    ) ) outbuf [i++] = 0;
  1360.     make_table((255  + 256     + 2 -  3    ) , outbuf , 12, d_buf );
  1361.     }
  1362. }
  1363.  
  1364. static  unsigned decode_c()
  1365. {
  1366.     unsigned j, mask;
  1367.  
  1368.     if (blocksize == 0) {
  1369.     blocksize = getbits(16);
  1370.     if (blocksize == 0) {
  1371.         return (255  + 256     + 2 -  3    ) ;  
  1372.     }
  1373.     read_pt_len(( 16   + 3) , 5  , 3);
  1374.     read_c_len();
  1375.     read_pt_len((   13     + 1) , 4  , -1);
  1376.     }
  1377.     blocksize--;
  1378.     j = d_buf [bitbuf >> ((8  * 2 * sizeof(char))  - 12)];
  1379.     if (j >= (255  + 256     + 2 -  3    ) ) {
  1380.     mask = (unsigned) 1 << ((8  * 2 * sizeof(char))  - 1 - 12);
  1381.     do {
  1382.         if (bitbuf & mask) j = (prev+0x8000     )   [j];
  1383.         else               j =  prev  [j];
  1384.         mask >>= 1;
  1385.     } while (j >= (255  + 256     + 2 -  3    ) );
  1386.     }
  1387.     fillbuf((int) outbuf [j]);
  1388.     return j;
  1389. }
  1390.  
  1391. static  unsigned decode_p()
  1392. {
  1393.     unsigned j, mask;
  1394.  
  1395.     j = pt_table[bitbuf >> ((8  * 2 * sizeof(char))  - 8)];
  1396.     if (j >= (   13     + 1) ) {
  1397.     mask = (unsigned) 1 << ((8  * 2 * sizeof(char))  - 1 - 8);
  1398.     do {
  1399.         if (bitbuf & mask) j = (prev+0x8000     )   [j];
  1400.         else               j =  prev  [j];
  1401.         mask >>= 1;
  1402.     } while (j >= (   13     + 1) );
  1403.     }
  1404.     fillbuf((int) pt_len[j]);
  1405.     if (j != 0) j = ((unsigned) 1 << (j - 1)) + getbits((int) (j - 1));
  1406.     return j;
  1407. }
  1408.  
  1409. static  void huf_decode_start()
  1410. {
  1411.     init_getbits();  blocksize = 0;
  1412. }
  1413.  
  1414.  
  1415.  
  1416.  
  1417.  
  1418. static  int j;     
  1419. static  int done;  
  1420.  
  1421. static  void decode_start()
  1422. {
  1423.     huf_decode_start();
  1424.     j = 0;
  1425.     done = 0;
  1426. }
  1427.  
  1428.  
  1429.  
  1430. static  unsigned decode(count, buffer)
  1431.     unsigned count;
  1432.     uch buffer[];
  1433.      
  1434.  
  1435.  
  1436.  
  1437.  
  1438.  
  1439.  
  1440.  
  1441. {
  1442.     static  unsigned i;
  1443.     unsigned r, c;
  1444.  
  1445.     r = 0;
  1446.     while (--j >= 0) {
  1447.     buffer[r] = buffer[i];
  1448.     i = (i + 1) & (((unsigned) 1 <<    13    )  - 1);
  1449.     if (++r == count) return r;
  1450.     }
  1451.     for ( ; ; ) {
  1452.     c = decode_c();
  1453.     if (c == (255  + 256     + 2 -  3    ) ) {
  1454.         done = 1;
  1455.         return r;
  1456.     }
  1457.     if (c <= 255 ) {
  1458.         buffer[r] = c;
  1459.         if (++r == count) return r;
  1460.     } else {
  1461.         j = c - (255  + 1 -  3    );
  1462.         i = (r - decode_p() - 1) & (((unsigned) 1 <<    13    )  - 1);
  1463.         while (--j >= 0) {
  1464.         buffer[r] = buffer[i];
  1465.         i = (i + 1) & (((unsigned) 1 <<    13    )  - 1);
  1466.         if (++r == count) return r;
  1467.         }
  1468.     }
  1469.     }
  1470. }
  1471.  
  1472.  
  1473.  
  1474.  
  1475.  
  1476. int unlzh(in, out)
  1477.     int in;
  1478.     int out;
  1479. {
  1480.     unsigned n;
  1481.     ifd = in;
  1482.     ofd = out;
  1483.  
  1484.     decode_start();
  1485.     while (!done) {
  1486.     n = decode((unsigned) ((unsigned) 1 <<    13    ) , window);
  1487.     if (!test && n > 0) {
  1488.         write_buf(out, (char*)window, n);
  1489.     }
  1490.     }
  1491.     return      0 ;
  1492. }
  1493.